package com.iskny.framework.model;

import java.io.Serializable;
import java.util.List;

import com.jfinal.plugin.activerecord.Page;
/**
 * Wrap of jfinal page to easy output
 * @author 伟强
 *
 */
@SuppressWarnings({"rawtypes","unchecked"})
public class MyPage implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1127065152633875077L;

	private int navNum = 10;//导航个数最大值
	private Page page;
	private int next;//
	private int prev;//
	private int beg;//
	private int end;//
	private int first=1;
	private int last;
	public MyPage(Page page) {
		this.page = page;
		this.last = getTotalPage();
		this.initParams();
		
	}
	
	private void initParams() {
		int num = getPageNumber();
		this.next = Math.min(this.last, num + 1);
		this.prev = Math.max(this.first, num - 1);
		this.beg = Math.max(num - this.navNum / 2, this.first);
		this.end = Math.min(this.beg + this.navNum - 1, this.last);
		if (this.end == this.last){//this.end - this.beg + 1 < this.navNum//导航数不足navNum
			this.beg = Math.max(this.first, this.end - this.navNum + 1);//beg前移navNum位，但不小于1
		}

	}
	
	public int getNext() {
		return next;
	}
	public void setNext(int next) {
		this.next = next;
	}
	public int getPrev() {
		return prev;
	}
	public Page getPage() {
		return page;
	}
	public int getBeg() {
		return beg;
	}
	public int getEnd() {
		return end;
	}
	
	public int getNavNum() {
		return navNum;
	}

	public int getFirst() {
		return first;
	}
	public int getLast() {
		return last;
	}
	/**
	 * Return list of this page.
	 */
	public <T>List<T> getList() {
		return page.getList();
	}
	
	/**
	 * Return page number.
	 */
	public int getPageNumber() {
		return page.getPageNumber();
	}
	
	/**
	 * Return page size.
	 */
	public int getPageSize() {
		return page.getPageSize();
	}
	
	/**
	 * Return total page.
	 */
	public int getTotalPage() {
		return page.getTotalPage();
	}
	
	/**
	 * Return total row.
	 */
	public int getTotalRow() {
		return page.getTotalRow();
	}

}
